package net.oschina.app.util;

import android.content.Context;
import android.content.res.Resources;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Handler;
import android.text.Spannable;
import android.text.method.LinkMovementMethod;
import android.text.style.URLSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import net.oschina.app.emoji.InputHelper;
import net.oschina.app.myapp.MyApplication;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static net.oschina.app.myapp.MyApplication.getMainThreadId;

/**
 * Created by wei03 on 2017/1/12.
 */

public class CommonUtils {
    //GMApplication相关的操作由CommonUtil来代理
    public  static Context getContext(){
        return MyApplication.getContext();
    }
    //取得资源
    public static Resources getResources(){
        return  getContext().getResources();
    }
    //dp转px  ,参照当前设备的屏幕密度
    public static  int dp2px(int dp){
        //手机屏幕密度 ：120dpi 低清  160dpi ：标清(中清)     240dpi ：高清    320dpi ：超高清
        //当前的density 是 当前手机屏幕密度与标清密度的比例值 ，比如 1， 1,5  2， 3
        float density = getResources().getDisplayMetrics().density;
        return (int) (density*dp+0.5);
    }
    //px转dp
    public  static  int px2dp(int px){
        float density = getResources().getDisplayMetrics().density;
        return (int) (px/density+0.5);
    }

    public static View inflate(int resId) {
        return View.inflate(getContext(), resId, null);
    }
    public static LayoutInflater getInflate() {
        return LayoutInflater.from(getContext());
    }
    //判断当前的线程是否为主线程
    public  static  boolean isMainThread(){
        return  getMainThreadId()==android.os.Process.myTid();
    }
    //取得Handler
    public static Handler getHandler(){
        return  MyApplication.getHandler();
    }
    public  static  void runOnMainThread(Runnable r){
        //假如当前就是主线程，则直接执行，否则通过Handler把任务发送到主线程的语句队列中，
        // 再由主线程的轮询器来取语句，再执行
        if(isMainThread()){
            //直接执行
            r.run();
        }else{
            getHandler().post(r);
        }
    }
    /**
     * 判断网络是否可用
     *
     * @param context Context对象
     */
    public static Boolean isNetworkReachable(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo current = cm.getActiveNetworkInfo();
        if (current == null) {
            return false;
        }
        return (current.isAvailable());
    }
    //富文本的展示
    public static void displayRichText(Context context, String body, TextView tvDes) {
        Spannable emoji = InputHelper.displayEmoji(context.getResources(), body);
        Pattern p = Pattern.compile("(http|ftp|https):\\/\\/[\\w\\-_]+(\\.[\\w\\-_]+)+([\\w\\-\\.,@?^=%&amp;:/~\\+#]*[\\w\\-\\@?^=%&amp;/~\\+#])?");
        Matcher m = p.matcher(emoji);
        while (m.find()) {
            URLSpan urlSpan = new URLSpan(m.group());
//            span = new ForegroundColorSpan(Color.RED);//需要重复！
//            span = new ImageSpan(drawable,ImageSpan.XX);//设置现在图片
            emoji.setSpan(urlSpan, m.start(), m.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            emoji.setSpan(emoji, m.end(), body.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        }
        tvDes.setText(emoji);
//        if (m.find()) {
            tvDes.setMovementMethod(new LinkMovementMethod());
//        }
    }
}




